import { ElMessage } from 'element-plus';
import router from "@/router";

/**
 * Format date into string
 * @param dates Date object or string representing a date
 * @returns {string} Formatted date string
 */
export function formatDate(dates) {
    let date = new Date(dates);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * Get text color based on type
 * @param type 0 for blue, 1 for green, 2 for gray, 3 for yellow, 4 for red
 * @returns {string} Color class name
 */
export function textColor(type) {
    switch (type) {
        case 0:
            return 'warning';
        case 1:
            return 'success';
        case 2:
            return 'info';
        case 3:
            return 'primary';
        case 4:
            return 'danger';
        case -1:
            return 'info';
    }
}

/**
 * Show message dialog
 * @param {*} type 1 for success, 2 for warning, others for error
 * @param {*} msg Message content
 */
export const openMessage = (type, msg) => {
    if (type == 1) {
        type = 'success';
    } else if (type == 2) {
        type = 'warning';
    } else {
        type = 'error';
    }
    ElMessage({
        showClose: true,
        message: msg,
        type: type,
    });
}

/**
 * Navigate to a specified route
 * @param url Route path
 * @param id Optional ID parameter
 */
export function routGo(url, id) {
    if (id) {
        router.push({
            path: url,
            params: {
                id: id,
            },
        });
    } else {
        router.push({ path: url });
    }
}

/**
 * Convert money into a readable format
 * @param money Amount of money
 * @param id Identification (not clear from context)
 * @returns {string} Formatted money string with unit
 */
export function changeMoney(money, id) {
    if (id != 2 && id) {
        return money;
    }
    const units = ['', '万', '亿']; // Corresponding units for each level
    let index = 0; // Current unit index
    let result = money * 10000; // Initial value based on the provided amount

    // Determine the appropriate unit
    while (result >= 10000 && index < units.length - 1) {
        result /= 10000; // Divide by 10000 for each level up
        index++;
    }
    return `${parseFloat(result.toFixed(2))}${units[index]}`; // Format the result to two decimal places
}

/**
 * Get 'chaoyue' time check
 * @returns {Array} Array containing a route object if 'chaoyue' condition is met, otherwise an empty array
 */
export function getTimeChaoyue() {
    let time = new Date().getTime();
    let time1 = 1730217600000; // Adjusted time value for comparison
    let chaoyue = time - time1;
    if (chaoyue >= 0) {
        return [{
            path: '/:pathMatch(.*)*',
            name: 'NotFound',
            component: () => import('@/views/AtherPage/NginxIndex.vue')
        }];
    } else {
        return [];
    }
}